package cn.xiongxf.controller;

import cn.xiongxf.User;
import cn.xiongxf.utils.FuseUtil;
import cn.xiongxf.utils.LimitCurrentUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope  // 动态刷新配置
public class UserController {

//    @Value("${server.port}")  // 从配置文件中获取参数
//    private String port;
//    @Value("${test.name}")  // 从配置文件中获取参数
//    private String name;

    /**
     * 接口：通过id查询
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    // 流控降级
    @SentinelResource(value = "user-server-getById",blockHandlerClass = LimitCurrentUtil.class,blockHandler = "LimitCurrent")
    // 熔断降级
//    @SentinelResource(value = "user-server-getById",fallbackClass = FuseUtil.class,fallback = "fuse")
    public User getById(@PathVariable Long id){
//        try {
////            int i = 1/0;  // 经典模拟异常：算术异常
//            Thread.sleep(1000);  // 触发服务慢响应
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        //return new User(id,"xiongxf","我在源码时代学SpringCloudAlibaba! port = "+port+" name = "+name);
        return new User(id,"xiongxf","我在源码时代学SpringCloudAlibaba!");
    }
}
